var express = require('express');
var router = express.Router();
var mysql = require('mysql');

var upload_dir = 'public/uploads/';

var multer = require('multer')
var upload = multer({dest:upload_dir})
var fs = require('fs')
var client = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '123',
    database:'blog',
    port: 3306
});
client.connect();
//取餐口

/*router.use(function (req, res, next) {
	if (req.session.is_login == true) {
		next();
	}else{
		res.redirect('/users/login')
	}
});*/

router.get('/',function(req,res,next){
	//看一下会员卡上用户是不是登录了
	if (req.session.is_login == true) {
	client.query('select *,articles.id as id,catalog.name as cata_name from articles left join catalog on articles.cata_id=catalog.id order by articles.id desc', function(err, rows, fields) {
		res.render("articleList",{articles:rows,username:req.session.username,success: req.flash('success')})
	})
	}else{
		res.redirect('/users/login')
	}
});

router.get("/addArticle",function(req,res,next){
	client.query('select * from catalog',function(err, rows, fields){
		res.render("addArticle",{catalog:rows,error1: req.flash('info1'),error2: req.flash('info2'),error3: req.flash('info3'),error4: req.flash('info4')});
	})
	
})

router.post('/addArticle',upload.single('image'),function(req,res,next){
		if (req.body.title=="" || req.body.content=="" || req.body.cata_id=="" || req.body.image=="") {
			if(req.body.title==""){
				req.flash('info1', "请输入标题") 
			}
			if(req.body.content==""){
				req.flash('info2', "请输入内容") 
			}
			if(req.body.cata_id==""){
				req.flash('info3', "请输入分类id")
			}
			if(req.body.image==""){
				req.flash('info4', "请输入图片")  
			}
			res.redirect('addArticle')
		}else{
		var sql = 'insert into articles values(null,"'+req.body.title+'","'+req.body.content+'","'+req.body.image+'","'+req.body.cata_id+'")'
		client.query(sql,function(err,rows,fields){
			req.flash('success', "添加成功") 
			res.redirect('/admin')
		})
	}
})

router.get("/delArticle",function(req,res,next){
	var sql = 'delete from articles where id = '+req.query.id;
	client.query(sql,function(err,rows,fields){
		res.redirect('/admin')
	})
})

router.get("/editArticle",function(req,res,next){
	client.query('select * from catalog',function(err, rows, fields){
		var catalogs = rows
		client.query('select * from articles where id ='+req.query.id,function(err, rows, fields){
			res.render("editArticle",{catalogs:catalogs,article:rows[0]});
		})
	})	
})

router.post("/editArticle",function(req,res,next){
	var sql = 'update articles set title="'+req.body.title+'",content="'+req.body.content+'",img="'+req.body.image+'",cata_id="'+req.body.cata_id+'" where id='+req.body.id;
	client.query(sql,function(err,rows,fields){
		res.redirect('/admin')
	})
})

router.get("/sign",function(req,res,next){
		res.redirect("/users/sign");
})

router.post("/uploadFile",upload.single('fileInput'),function(req,res,next){
	fs.renameSync(upload_dir+req.file.filename,upload_dir+req.file.originalname)
	var img = 'uploads/'+req.file.originalname
	res.send(img)	
})

router.get("/logout",function(req,res,next){
	req.session.destroy()
	res.redirect('/users/login')
})

router.get("/text",function(req,res,next){
	res.send('123')
})
module.exports = router;
